home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / qdeck / sockets / uclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-11  |  2.6 KB  |  119 lines

  1. /*****************************************************************************
  2. *  
  3. *                                  DESQview/X 
  4. *                           BASIC DATAGRAM DAEMON TESTER
  5. *
  6. *  The following code is used to test the basic datagram daeomon (UDAEMON.C).
  7. *
  8. *
  9. ******************************************************************************/
  10.  
  11.  
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include <netdb.h>        
  17. #include <netinet\in.h>    
  18. #include <sys\time.h>
  19. #include <sys\socket.h>
  20. #include <sys\errno.h>
  21. #include <sys\ioctl.h>
  22.  
  23.  
  24. #define TEST_SERVICE    "udptest"
  25.  
  26. void    chat(int s);
  27.  
  28. void main(int argc, char *argv[])
  29. {
  30.     int        s;
  31.     struct    sockaddr_in    addr;
  32.     struct    hostent        *h;
  33.     struct    servent        *serv;
  34.     char                        sbuff[81];
  35.  
  36.     if(argc < 2){
  37.         printf("\n\nUsage %s <host>\n\n",argv[0]);
  38.         exit(1);
  39.     }
  40.  
  41.     /* Open a datagram (UDP) socket */
  42.  
  43.     s = socket(AF_INET,SOCK_DGRAM,0);
  44.  
  45.     if(s < 0){
  46.  
  47.         printf("\nError:  Could not open datagram socket.\n");
  48.  
  49.         exit(2);
  50.     }
  51.  
  52.     /* Bind (name) the socket.  A zero address tells the kernel to */
  53.     /* name the socket giving it the sin_addr of the local host      */
  54.     /* with any available port.                                                */
  55.  
  56.     memset(&addr, 0, sizeof(struct sockaddr_in));
  57.  
  58.     addr.sin_family = AF_INET;
  59.  
  60.     if(bind(s,(struct sockaddr *) &addr,sizeof(addr))){
  61.  
  62.         printf("\nError:  Could not bind datagram socket.\n");
  63.  
  64.         exit(3);
  65.  
  66.     }        
  67.         
  68.  
  69.     /* Retrieve the information about the port (service) that we    */
  70.     /* wish to send the datagram to.                                            */
  71.  
  72.     serv = getservbyname(TEST_SERVICE,"udp");
  73.  
  74.     if(serv == NULL){
  75.  
  76.         printf("\nError:  Unable to retrieve information for %s service.\n",TEST_SERVICE);
  77.  
  78.         exit(4);
  79.     }
  80.  
  81.     /* Now get the host (address) information for the destination    */
  82.     /* host and fill the destination address structure giving it    */
  83.     /* the address from the host information and the port from        */
  84.     /* the retrieved service information.                                    */
  85.  
  86.     h = gethostbyname(argv[1]);
  87.  
  88.     if(h == NULL){
  89.  
  90.         printf("\nError:  Unknown host name specified.\n");
  91.  
  92.         exit(5);
  93.     }
  94.  
  95.     memset(&addr,0,sizeof(struct sockaddr_in));
  96.  
  97.     addr.sin_family    = AF_INET;
  98.     addr.sin_addr        = *((struct in_addr *)(h->h_addr));
  99.     addr.sin_port        = serv->s_port;
  100.             
  101.  
  102.     /* Get some input from the user and send a datagram to the         */
  103.     /* destination daemon.                                                        */
  104.  
  105.     printf("\nEnter your message:\n\n");
  106.  
  107.     gets(sbuff);
  108.  
  109.     if(strlen(sbuff) > 0){
  110.     
  111.         sendto(s, sbuff, strlen(sbuff) + 1, 0,(struct sockaddr *) &addr, sizeof(addr));
  112.  
  113.         printf("\n\nMessage sent.\n\n");
  114.  
  115.     }
  116.     so_close(s);
  117. }
  118.  
  119.